/* @name：北京建筑行业，再次合并
	@parameter:originalData 多个资质的原数组
	@parameter:endObjData  北京计算前的数据，参考下面传入格式示例
	@parameter:firstBuildId  一级建造师id
	@parameter:secondBuildId  二级建造师id
	@parameter:engineerIds  可去除的工程师ids
//传入格式示例
endObjData = {
	'11-2':1,    //一级建造师
	'11-1':10,    //二级建造师
	'20-363':1,  //工程师-结构
	'20-54':2,   //工程师-给排水
	'20-53':2,    //工程师-暖通
	'20-100':20,
}
*/

export default function(originalData,endObjData,firstBuildId,secondBuildId,engineerIds){
	if( !(originalData && endObjData && firstBuildId && secondBuildId && engineerIds) ){
		return endObjData || {}
	};
	let endBeijingObj = {...endObjData},
		originalLength = originalData.length;
	let bookHalfLength = originalLength ? Math.ceil(originalLength/2) : 1,    //可以工程师需保留的数量（资质总数的一半）
		endObjKeys = Object.keys(endObjData);			
	//建筑工程专业一级建造师
	let	firstBuildBool = endObjKeys.includes(firstBuildId),
		secondBuildBool = endObjKeys.includes(secondBuildId);
	if(
		( firstBuildBool || secondBuildBool ) && 
		endObjKeys.some(someItem=>{
			return engineerIds.includes(someItem);
		})
	){
		let firstBuildNumber = endObjData[firstBuildId] ? endObjData[firstBuildId] : 0,  //合并前一级建造师数量
			secondBuildNumber = endObjData[secondBuildId] ? endObjData[secondBuildId] : 0;  //合并前二级建造师数量
		let engineerObj = {},  //合并前符合规则工程师obj
			engineerCount = 0;  //合并前符合规则工程师总数量
		engineerIds.forEach(item=>{
			if( endObjData[item] ){
				engineerCount+=endObjData[item];
				engineerObj[item] = endObjData[item];
			};
		});
		let computeBeijingObj = {...engineerObj};  //北京最终的数据对象（不含不符合的工程师和建造师）
		if( engineerCount>bookHalfLength ){
			let canEngineerCount = engineerCount-bookHalfLength;  //总工程师和最小可去除工程师（资质总数的一半）数量差
			if( canEngineerCount ){
				let canEngineerNumber = 0;  //可以去除工程师的数量
				if( firstBuildBool && secondBuildBool ){
					//同时有一二级建造师
					if( canEngineerCount>firstBuildNumber ){
						let allBuildCount = firstBuildNumber+secondBuildNumber;
						if( canEngineerCount<allBuildCount ){
							canEngineerNumber = canEngineerCount;
							let canAndBuildEifference = canEngineerCount-firstBuildNumber;
							computeBeijingObj[firstBuildId] = firstBuildNumber+canAndBuildEifference;
							computeBeijingObj[secondBuildId] = secondBuildNumber-canAndBuildEifference;
						}else{
							canEngineerNumber = allBuildCount;
							computeBeijingObj[firstBuildId] = allBuildCount;
						};
					}else{
						canEngineerNumber = canEngineerCount;
						computeBeijingObj[firstBuildId] = firstBuildNumber;
						computeBeijingObj[secondBuildId] = secondBuildNumber;
					};
				}else{
					if( firstBuildBool ){
						//只有一级建造师
						canEngineerNumber = canEngineerCount-firstBuildNumber>-1 ? firstBuildNumber : canEngineerCount;
						computeBeijingObj[firstBuildId] = firstBuildNumber;
					}else{
						//只有二级建造师
						if( canEngineerCount<secondBuildNumber ){
							canEngineerNumber = canEngineerCount;
							let buildDifference = secondBuildNumber-canEngineerCount;
							computeBeijingObj[firstBuildId] = canEngineerCount;
							computeBeijingObj[secondBuildId] = buildDifference;
						}else{
							canEngineerNumber = secondBuildNumber;
							computeBeijingObj[firstBuildId] = secondBuildNumber;
						};
					};
				};
				//循环扣除工程师数量
				for( let item in computeBeijingObj ){
					let difference = canEngineerNumber-computeBeijingObj[item];
					if( difference>-1 ){
						canEngineerNumber = difference;
						delete computeBeijingObj[item];
					}else{
						computeBeijingObj[item] = computeBeijingObj[item]-canEngineerNumber;
						break;
					}
				};
				//最后合并数据
				[firstBuildId,secondBuildId,...engineerIds].forEach(item=>{
					if( computeBeijingObj[item] ){
						endBeijingObj[item] = computeBeijingObj[item];
					}else{
						delete endBeijingObj[item];
					};
				});
			};
		};
	};
	console.log('北京单次计算的数据',endBeijingObj);
	return endBeijingObj;
}